﻿using System.Xml.Linq;
using System.Linq;

namespace AllTalk
{
    public static class XEtensions
    {
        public static string GetAttribute(this XElement element, XName attributeName)
        {
            return element.GetAttribute(attributeName, string.Empty);
        }

        public static string GetAttribute(this XElement element, XName attributeName, string defaultValue)
        {
            XAttribute attribute = element.Attribute(attributeName);
            if (attribute != null)
            {
                return attribute.Value;
            }
            return defaultValue;
        }

        public static string GetElement(this XElement element, params XName[] elementNames)
        {
            XElement currentElement = element;
            foreach (XName elementName in elementNames)
            {
                currentElement = currentElement.Elements(elementName).FirstOrDefault();
                if (currentElement == null)
                {
                    return string.Empty;
                }
            }
            return currentElement.Value;
        }
    }
}
